package com.fingard.dsp.bank.directbank.cmbc01;

import com.fingard.dsp.bank.directbank.bankInterface.IUndueBillInfo;
import com.fingard.dsp.bank.reqrespobj.qryunduebillinfo.ReqQryUndueBillInfo;
import com.fingard.dsp.bank.reqrespobj.qryunduebillinfo.RetAtsUndueBillInfo;
import com.fingard.dsp.bank.reqrespobj.qryunduebillinfo.RetAtsUndueBillInfoDetail;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;
import org.springframework.stereotype.Component;

/**
 * 获取未到期票据信息
 *
 * @author marlon
 * @date 2019/1/16
 */
@Component("CMBC01QryUndueBillInfo")
public class CMBC01QryUndueBillInfo extends CMBC01Base implements IUndueBillInfo {
    @Override
    public RetAtsUndueBillInfo qryUndueBillInfo(ReqQryUndueBillInfo undueBillInfo) throws Exception {
        RetAtsUndueBillInfo retAts = new RetAtsUndueBillInfo(undueBillInfo.reqHeader);
        retAts.writeStart();
        try {
            CMBC01Header bankHeader = new CMBC01Header();
            bankHeader.trnCode = "XR205013";
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入通用的头部节点
            writeCommonHeader(xmlWriter, bankHeader);
            XElement tmpXElement = new XElement("xDataBody", "");
            tmpXElement.addChild(new XElement("dealerCode", undueBillInfo.DealerCode));
            tmpXElement.addChild(new XElement("financeStartDateStart", ""));
            tmpXElement.addChild(new XElement("financeStartDateEnd", ""));
            tmpXElement.addChild(new XElement("financeStatus", "0"));
            xmlWriter.writeXElement(tmpXElement);
            xmlWriter.writeEndDocument();//写入报文完成

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());//发送银行
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getFirstText("/CMBC/responseHeader/status/code");
                    String tmpRetMessage = tmpBkReader.getFirstText("/CMBC/responseHeader/status/message");
                    if ("0".equals(tmpRetCode)) {
                        retAts.writeRespCode("0");
                        retAts.writeRespInfo(tmpRetMessage);
                        while (tmpBkReader.read("/CMBC/xDataBody/Detail")) {
                            RetAtsUndueBillInfoDetail tmpRetDetail = new RetAtsUndueBillInfoDetail();
                            tmpRetDetail.DealerCode = undueBillInfo.DealerCode;
                            tmpRetDetail.BillNo = tmpBkReader.readedXElement.getSingleText("financeNo");
                            tmpRetDetail.BillMoney = tmpBkReader.readedXElement.getSingleText("financeAmount");
                            tmpRetDetail.BillStartDate = tmpBkReader.readedXElement.getSingleText("startDate").replace("-","");
                            tmpRetDetail.BillEndDate = tmpBkReader.readedXElement.getSingleText("endDate").replace("-","");
                            tmpRetDetail.BailMoney = tmpBkReader.readedXElement.getSingleText("initialDepositAmount");
                            tmpRetDetail.RepaymentMoney = tmpBkReader.readedXElement.getSingleText("repaiedAmount");
                            tmpRetDetail.BailAndRepaymentMoney = tmpBkReader.readedXElement.getSingleText("additionalDepositAmount");
                            tmpRetDetail.Reserve1 = "";
                            tmpRetDetail.Reserve2 = "";
                            tmpRetDetail.Reserve3 = "";
                            tmpRetDetail.Reserve4 = "";
                            tmpRetDetail.Reserve5 = "";
                            retAts.writeRetDetail(tmpRetDetail);
                        }
                    } else {
                        retAts.writeRespCode("-1");
                        retAts.writeRespInfo(tmpRetMessage);
                    }
                } else {
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(tmpSbMsg.toString());
                }
            } else {
                retAts.writeRespCode("-1");
                retAts.writeRespInfo(tmpStrRet[1]);
            }

        } catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
        }
        retAts.writeEnd();
        return retAts;

    }
}



